<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Title</title>
</head>
<body>
    <div id="app" style="margin: 100px">
        <parent></parent>
    </div>
    <template id="par">
        <div>
            <h1>{{parentMsg}}:{{message}}</h1>
            <child1 @change="getVal"></child1>
        </div>
    </template>

    <template id="c1">
        <div>
            <button @click="fn">
                点击传值组件
            </button>
            <p>准备传递的数据:{{cMsg1}}</p>
        </div>
    </template>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script type="text/javascript">
    var vm = new Vue({
        el: "#app",
        data: {

        },
        components: {
            "parent": {
                template: "#par",
                data(){
                    return {
                        parentMsg: "父组件接收的信息",
                        message: ""
                    }
                },
                methods: {
                    getVal: function(val) {
                        // val:代表传递过来的值
                        this.message = val;
                        console.log("接收的信息为:"+this.message);
                    }
                },
                components: {
                    "child1": {
                        template: "#c1",
                        data(){
                            return{
                                cMsg1: "1000"
                            }
                        },
                        methods: {
                            fn: function() {
                                //自定事件
                                this.$emit("change",this.cMsg1)
                            }
                        }
                    }
                }
            }
        }
    })
</script>
</html>